<?php
/**
 * 功能: 积分兑换商品
 * User: 文丹
 * Date: 2018/7/25 0025
 * Time: 16:59
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;

class Convert extends Power{

    public function index(){
        $goods = Db::name('product')->order('id desc')->paginate(10)->each(function($item,$key){
            $item['img'] = array_filter(explode(',',$item['image']));
            return $item;
        });
       /* echo "<pre>";
        print_r($goods);
        echo "</pre>";*/
        $count = Db::name('product')->count();
        $this->assign([
           'goods' =>$goods,
           'count' =>$count,
            'empty'=>'<tr class="text-c va-m empty"><td colspan="9">暂无数据</td></tr>'
        ]);
        return view('');
    }

    /**
     * 产品搜索
     */
    public function product_search(){
        $title = trim(input('title'));
        if(empty($title)){
            return json(['msg'=>'搜索条件不能为空','ok'=>false]);
        }
        $goods = Db::name('product')
            ->where('title','like','%'.$title.'%')
            ->paginate(10)->each(function($item,$key){
            $item['img'] = array_filter(explode(',',$item['image']));
            return $item;
        });
         /*echo "<pre>";
         print_r($goods);
         echo "</pre>";
        die;*/
        $count = Db::name('product')->where('title','like','%'.$title.'%')->count();
        if(!empty($goods[0])){
            $this->assign([
                'goods' =>$goods,
                'count' =>$count
            ]);
            return $this->fetch('convert:index',['title'=>$title]);
        }else{
            return json(['msg'=>'没有符合条件的结果','ok'=>false]);
        }

    }

    /**
     * 产品添加
     */
    public function goods_add(){
        $id = intval(input('id'));

        if($id){
            if($id<=0){
                return json(['msg'=>'id必须为正整数！','ok'=>false]);
            }
            $product = Db::name('product')->where('id',$id)->find();
            $product['img'] = array_filter(explode(',',$product['image']));
            if($product){
                $this->assign('product',$product);
            }else{
                return json(['msg'=>'服务器内部错误！','ok'=>false]);
            }

        }

        return $this->fetch('convert:goods-add');
    }
    public function goods_hold(){
        $title    = trim(input('title'));
        $integral = intval(input('integral'));
        $unit     = trim(input('unit'));
        $s_price  = trim(input('s_price'));
        $images   = trim(input('image'));
        $intro    = input('intro');
        $detail   = input('detail');
        $stock    = intval(input('stock'));

        if(empty($title) || empty($integral) || empty($unit) ||empty($s_price) ||empty($images) ){
            return json(['msg'=>'必填项不能为空！','ok'=>false]);
        }

        $data = [
            'title'=>$title,
            'integral'=>$integral,
            'unit'=>$unit,
            's_price'=>$s_price,
            'image'=>$images,
            'intro'=>$intro,
            'detail'=>$detail,
            'stock'=>$stock,
            'time'=>time()
        ];
        $result = Db::name('product')->insert($data);
        if($result){
            return json(['msg'=>'添加成功！','ok'=>true]);
        }else{
            return json(['msg'=>'添加失败！','ok'=>false]);
        }
    }

    /**
     *  图片上传
     */
    public function upload(){
        $file = request()->file('file');
        // 移动到框架应用根目录/public/farmer/ 目录下
        $info = $file->validate(['size' => 2097152, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'goods');

        $Img = "";
        // $arr = array();
        if ($info) {
            $Img = $info->getSaveName();
            $image = str_replace("\\","/",$Img);//将图片路径右斜杠' \ '改为左斜杠 ' / '
            return json(['msg'=>'上传成功','ok'=>true,'image'=>$image]);
        } else {
            // 上传失败获取错误信息
            $error = $file->getError();
            return json(['msg'=>'上传失败'.$error,'ok'=>false]);
        }
    }

    public function goods_edit(){
        $id       = trim(input('product_id'));
        $title    = trim(input('title'));
        $integral = intval(input('integral'));
        $unit     = trim(input('unit'));
        $s_price  = trim(input('s_price'));
        $images   = trim(input('image'));
        $intro    = input('intro');
        $detail   = input('detail');
        $stock    = intval(input('stock'));

        if(empty($title) || empty($integral) || empty($unit) ||empty($s_price) ||empty($images) ){
            return json(['msg'=>'必填项不能为空！','ok'=>false]);
        }

        $data = [
            'title'=>$title,
            'integral'=>$integral,
            'unit'=>$unit,
            's_price'=>$s_price,
            'image'=>$images,
            'intro'=>$intro,
            'detail'=>$detail,
            'stock'=>$stock,
            'time'=>time()
        ];
        $result = Db::name('product')->where('id',$id)->update($data);
        if($result){
            return json(['msg'=>'修改成功！','ok'=>true]);
        }else{
            return json(['msg'=>'修改失败！','ok'=>false]);
        }
    }

    /**
     * 产品下架
     */
    public function down(){
        $id = intval(input('id'));
        $result = Db::name('product')->where('id',$id)->update(['is_down'=>2]);
        if($result){
            return json(['msg'=>'已下架！','ok'=>true]);
        }else{
            return json(['msg'=>'下架失败！','ok'=>false]);
        }
    }

    /**
     * 产品发布
     */
    public function up(){
        $id = intval(input('id'));
        $result = Db::name('product')->where('id',$id)->update(['is_down'=>1]);
        if($result){
            return json(['msg'=>'已发布！','ok'=>true]);
        }else{
            return json(['msg'=>'发布失败！','ok'=>false]);
        }
    }

    /**
     * 删除单个
     */
    public function product_del(){
        $id = intval(input('id'));
        $result = Db::name('product')->where('id',$id)->delete();
        if($result){
            return json(['msg'=>'已删除！','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败！','ok'=>false]);
        }
    }

    /**
     * 批量删除
     */
    public function productDatadel(){
        $ids = input('ids/a');
       // print_r($ids);
        $result = Db::name('product')->whereIn('id',$ids)->delete();
        if($result){
            return json(['msg'=>'已删除！','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败！','ok'=>false]);
        }
    }


}